const { formatDateTime } = require('../../../utils/date')
const financeUserStatistics = require('../../../utils/finance-user-statistics')

Page({
  data: {
    userStatistics: {
      accumulativeDays: 0,
      currentStreakDays: 0,
      lastRecordTime: '-',
      daysSinceLastRecord: '-'
    },
    rippleKey: '',
    profileList: [
      { key: 'family', title: '我的家庭', icon: '/assets/finance/finance-sys-house.svg' },
      { key: 'goal', title: '我的目标', icon: '/assets/finance/finance-sys-social.svg' },
      { key: 'category', title: '我的分类', icon: '/assets/finance/finance-sys-shop.svg' },
      { key: 'account', title: '我的账户', icon: '/assets/finance/finance-sys-salary.svg' },
      { key: 'payment-method', title: '支付方式', icon: '/assets/finance/finance-sys-financial.svg' },
      { key: 'template', title: '收支模板', icon: '/assets/finance/finance-sys-financial.svg' }
    ],
  },
  async onLoad() {
    // 加载用户统计数据
    await this.loadUserStatistics();
  },
  /**
   * 加载用户记账统计数据
   */
  async loadUserStatistics() {
    try {
      const res = await financeUserStatistics.getUserStatistics();
      if (res) {
        const userStatistics = {
          accumulativeDays: res.accumulativeDays || 0,
          currentStreakDays: res.currentStreakDays || 0,
          lastRecordTime: res.lastRecordTime ? formatDateTime(res.lastRecordTime) : '-',
          daysSinceLastRecord: res.daysSinceLastRecord || 0
        };
        this.setData({ userStatistics });
      }
    } catch (e) {
      console.error('获取用户统计数据失败:', e);
      // 不显示错误提示，保持默认值
    }
  },
  onProfileItemTap(e) {
    const key = e.currentTarget.dataset.key;
    this.setData({ rippleKey: key });
    setTimeout(() => {
      this.setData({ rippleKey: '' });
    }, 500);
    // 跳转或弹窗
    switch (key) {
      case 'family':
        wx.navigateTo({ url: '/pages/finance/profile/family/family' });
        break;
      case 'goal':
        wx.navigateTo({ url: '/pages/finance/profile/my-goal/my-goal' });
        break;
      case 'category':
        wx.navigateTo({ url: '/pages/finance/profile/category/category' });
        break;
      case 'account':
        wx.navigateTo({ url: '/pages/finance/profile/account/account' });
        break;
      case 'payment-method':
        wx.navigateTo({ url: '/pages/finance/profile/payment-method/payment-method' });
        break;
      case 'template':
        wx.navigateTo({ url: '/pages/finance/profile/template/template' });
        break;
    }
  },
}); 